<?php

use think\captcha\Captcha;
use think\Db;

/**
 * @name:
 * @test: test font
 * @msg:
 * @param {type}
 * @return:
 */
function dbmodel($table)
{
    return db($table);
}

/**
 *json固定格式1
 */
function outputJson($code, $message = '', $content = null)
{
    $data['code'] = $code;
    $data['message'] = $message ? $message : lang('code_' . $code);
    $data['data'] = $content;
    return json_encode($data);
}


/**
 *PHP stdClass Object转array
 *
 * @return array
 */
function objectToArray($array)
{
    if (is_object($array)) {
        $array = (array)$array;
    }
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = objectToArray($value);
        }
    }
    return $array;
}

/**
 * 二维数组排序；
 * @param string $prefix 前缀
 * @param array $arr 排序数组
 * @param array $shortKey 排序字段
 * @param array $short 排序方式
 * @param array $shortType 排序类型
 * @return  array
 */
function multi_array_sort($arr, $shortKey, $short = SORT_DESC, $shortType = SORT_REGULAR)
{
    foreach ($arr as $key => $data) {
        $name[$key] = $data[$shortKey];
    }
    array_multisort($name, $shortType, $short, $arr);
    return $arr;
}


//PHP stdClass Object转array
function object_array($array)
{
    if (is_object($array)) {
        $array = (array)$array;
    }
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = object_array($value);
        }
    }
    return $array;
}

function birthday($birthday)
{
    $age = strtotime($birthday);
    if ($age === false) {
        return false;
    }
    list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age));
    $now = strtotime("now");
    list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now));
    $age = $y2 - $y1;
    if ((int)($m2 . $d2) < (int)($m1 . $d1))
        $age -= 1;
    return $age;
}

function getConstellation($birthday)
{

//    传入$birthday格式如：2018-05-06

    $month = intval(substr($birthday, 5, 2));
    $day = intval(substr($birthday, 8, 2));
    if ($month < 1 || $month > 12 || $day < 1 || $day > 31) {
        return NULL;
    }
    $signs = array(
        array('20' => '水瓶座'),
        array('19' => '双鱼座'),
        array('21' => '白羊座'),
        array('20' => '金牛座'),
        array('21' => '双子座'),
        array('22' => '巨蟹座'),
        array('23' => '狮子座'),
        array('23' => '处女座'),
        array('23' => '天秤座'),
        array('24' => '天蝎座'),
        array('22' => '射手座'),
        array('22' => '摩羯座')
    );
    list($start, $name) = fun_adm_each($signs[$month - 1]);
    if ($day < $start) {
        list($start, $name) = fun_adm_each($signs[($month - 2 < 0) ? 11 : $month - 2]);
    }

    return $name;
}

function fun_adm_each(&$array)
{
    $res = array();
    $key = key($array);
    if ($key !== null) {
        next($array);
        $res[1] = $res['value'] = $array[$key];
        $res[0] = $res['key'] = $key;
    } else {
        $res = false;
    }
    return $res;
}

function curlGet($url, $timeOut = 5, $aHeader = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(array('Content-Type:application/json'), $aHeader));
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);          //单位 秒，也可以使用
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        $data = curl_error($ch);
    }
    curl_close($ch);
    return $data;
}


/**
 * post请求
 *
 * @param [type] $url
 * @param [type] $data
 * @return void
 */
function curlPost($url, $data, $timeOut = 5, $aHeader = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(array('Content-Type:application/json'), $aHeader));
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);          //单位 秒，也可以使用
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        $data = curl_error($ch);
    }
    curl_close($ch);
    return $data;
}
/**
 * post请求
 *
 * @param [type] $url
 * @param [type] $data
 * @return void
 */
function curlPosts($url, $data, $timeOut = 5, $aHeader = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);          //单位 秒，也可以使用
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3000); //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    $output = curl_exec($ch);
    curl_close($ch);
    $result = object_arrays(json_decode($output));
    return $result;
}

function time_tran($the_time)
{
    $t = time() - strtotime($the_time);
    $f = array(
        '31536000' => '年',
        '2592000' => '个月',
        '604800' => '星期',
        '86400' => '天',
        '3600' => '小时',
        '60' => '分钟',
        '1' => '秒'
    );
    foreach ($f as $k => $v) {
        if (0 != $c = floor($t / (int)$k)) {
            return $c . $v . '前';
        }
    }
}

//阿里云短信发送
function sendAliyunSms($mobile, $code){
    vendor('Alimsg/api_sdk/Sms');
    $sms = new \Sms;
    $msgArr = ['code'=>$code];
    $return = $sms->sendSms(['msg'=> $msgArr, 'template'=>config('templates'), 'phone'=>$mobile]);
    return $return;
}